﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Diagnostics;

namespace TrainStation
{
    public class Dispatcher
    {
        /// <summary>
        /// Miliseconds
        /// </summary>
        public int Interval { get; protected set; }
        public RailRoad RailRoad { get; set; }
        Stopwatch timer;

        public bool IsRunning { get; protected set; }

        public Dispatcher( RailRoad railroad, int Interval = 100 )
        {
            this.Interval = Interval;
            this.RailRoad = railroad;
            this.timer = new Stopwatch();
        }

        public void Start()
        {
            IsRunning = true;
            //DateTime lastUpdate = DateTime.Now.AddMilliseconds(-Interval);
            timer.Start();
            while (IsRunning)
            {
                if (timer.ElapsedMilliseconds > Interval)
                {
                    RailRoad.UpdateStates(Interval);
                    timer.Restart();
                }
            }
        }
    }
}
